import Vue from 'vue'
import VueRouter, { RouteConfig } from 'vue-router'

/* Layout */
import Layout from '@/layout/index.vue'

/* Router modules */
import workOrder from './modules/work-order'
import resource from './modules/resource';
import desktopManage from './modules/desktopManage'
import session from './modules/cloudSession'
import client from './modules/cloudClient'
import rules from './modules/rules'
import user from './modules/user'
import tenant from './modules/tenant'
import systemManage from './modules/system'
import overview from './modules/overview';
import { AuthModule } from '../store/modules/auth';
import policy from './modules/policy';
import ScheduledTasks from './modules/scheduledTasks';
import logManage from './modules/log';
import order from './modules/order';
import faqSettingRouter from './modules/markdownManage';
import agreementRouter  from './modules/agreement'
import SystemSettingTenant from './modules/system_tenant'
import workSpace from './modules/workspace'

Vue.use(VueRouter)

/*
  Note: sub-menu only appear when children.length>=1
  Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
*/

/*
  name:'router-name'             the name field is required when using <keep-alive>, it should also match its component's name property
                                 detail see : https://vuejs.org/v2/guide/components-dynamic-async.html#keep-alive-with-Dynamic-Components
  redirect:                      if set to 'noredirect', no redirect action will be trigger when clicking the breadcrumb
  meta: {
    roles: ['admin', 'editor']   will control the page roles (allow setting multiple roles)
    title: 'title'               the name showed in subMenu and breadcrumb (recommend set)
    icon: 'svg-name'             the icon showed in the sidebar
    hidden: true                 if true, this route will not show in the sidebar (default is false)
    alwaysShow: true             if true, will always show the root menu (default is false)
                                 if false, hide the root menu when has less or equal than one children route
    breadcrumb: false            if false, the item will be hidden in breadcrumb (default is true)
    noCache: true                if true, the page will not be cached (default is false)
    affix: true                  if true, the tag will affix in the tags-view
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
*/

/**
  ConstantRoutes
  a base page that does not have permission requirements
  all roles can be accessed
*/
export const constantRoutes: RouteConfig[] = [
  overview,
  resource,
  workSpace,
  desktopManage,
  client,
  session,
  workOrder,
  policy,
  order,
  logManage,
//  ScheduledTasks,
//  logManage,
  tenant,
  user,
  systemManage,
  rules,
  faqSettingRouter,
  agreementRouter,
  SystemSettingTenant,
  {
    path: '/login/system_admin',
    name: 'SystemAdminLogin',
    component: () => import(/* webpackChunkName: "login" */ '@/views/login/index.vue'),
    meta: { hidden: true }
  },
  {
    path: '/login/tenant_admin',
    name: 'TenantAdminLogin',
    component: () => import(/* webpackChunkName: "login" */ '@/views/login/index.vue'),
    meta: { hidden: true }
  },
  {
    path: '/403',
    name: '403',
    component: () => import(/* webpackChunkName: "login" */ '@/views/err_code/403.vue'),
    meta: { hidden: true }
  },
  {
    path: '/notopened',
    name:'notopened',
    component : () => import('@/views/err_code/notopened.vue'),
    meta: { hidden: true }
  },
  {
    path: '/ram_forbidden',
    name: 'RamForbidden',
    component: () => import(/* webpackChunkName: "login" */ '@/views/err_code/ramForbidden.vue'),
    meta: { hidden: true }
  },
  {
    path: '/faq',
    name: 'faq',
    component: () => import(/* webpackChunkName: "login" */ '@/views/markdown/index.vue'),
    meta: { hidden: true },
  },
  {
    path: "/faq/content/",
    component: () => import("@/views/markdown/content.vue"),
    name: "Content",
    meta: { hidden: true }
  },
  {
    path: "/faq/agreement/",
    component: () => import("@/views/markdown/agreement.vue"),
    name: "Agreement",
    meta: { hidden: true }
  },
  {
    path: "/faq/policy/",
    component: () => import("@/views/markdown/policy.vue"),
    name: "Policy",
    meta: { hidden: true }
  },
  {
    path: "/faq/protocol/",
    component: () => import("@/views/markdown/protocol.vue"),
    name: "Protocol",
    meta: { hidden: true }
  },
  {
    path: '/',
    redirect: '/overview/index',
    component: Vue.component('xxx', {
      render(h) {
        return h('span', undefined, null);
      },
      mounted() {
        this.$router.push({
          path: AuthModule.authrole === 'system_admin' ? '/resource/pool' : '/desktopManage/DesktopPools'
        })
      }
    })
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
*/
// export const asyncRoutes: RouteConfig[] = []

const createRouter = () => new VueRouter({
  // mode: 'history',  // Disabled due to Github Pages doesn't support this, enable this if you need.
  scrollBehavior: (to, from, savedPosition) => {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  },
  base: process.env.BASE_URL,
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  (router as any).matcher = (newRouter as any).matcher // reset router
}

export default router
